package com.ruoyi.web.controller.project;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目信息Controller
 *
 * @author ruoyi
 * @date 2025-07-11
 */
@RestController
@RequestMapping("/project/project")
@Api(tags = "项目管理接口") // Swagger 整体描述
public class ProjectController extends BaseController {

    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    @ApiOperation("查询项目列表") // 接口描述
    public TableDataInfo list(@ApiParam("项目查询条件") Project project) {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出项目列表")
    public void export(HttpServletResponse response, @ApiParam("项目查询条件") Project project) {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<>(Project.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取项目详情")
    public AjaxResult getInfo(@ApiParam("项目ID") @PathVariable("id") Long id) {
        return success(projectService.selectProjectById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增项目")
    public AjaxResult add(@ApiParam("新增项目信息") @RequestBody @Valid Project project) {
        project.setCreateBy(getUsername());
        project.setUserId(getUserId());
        project.setDeptId(getDeptId());
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改项目")
    public AjaxResult edit(@ApiParam("修改项目信息") @RequestBody @Valid Project project) {
        project.setUpdateBy(getUsername());
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除项目")
    public AjaxResult remove(@ApiParam("项目ID数组") @PathVariable Long[] ids) {
        return toAjax(projectService.deleteProjectByIds(ids));
    }
}
